{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wJcYs_ERTnnI"
      },
      "source": [
        "##### Copyright 2021 The TensorFlow Authors."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "HMUDt0CiUJk9"
      },
      "outputs": [],
      "source": [
        "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "77z2OchJTk0l"
      },
      "source": [
        "# Tracking metrics with TensorBoard\n",
        "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://www.tensorflow.org/guide/migrate/tensorboard\">\n",
        "    <img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />\n",
        "    View on TensorFlow.org</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/guide/migrate/tensorboard.ipynb\">\n",
        "    <img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />\n",
        "    Run in Google Colab</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://github.com/tensorflow/docs/blob/master/site/en/guide/migrate/tensorboard.ipynb\">\n",
        "    <img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />\n",
        "    View source on GitHub</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a href=\"https://storage.googleapis.com/tensorflow_docs/docs/site/en/guide/migrate/tensorboard.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
        "  </td>\n",
        "</table>\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yeZymcHHFE3g"
      },
      "source": [
        "## Migrating Tensorboard metric displays from TF1 to TF2"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hIo_p2FWFIRx"
      },
      "source": [
        "`TensorBoard` is a built-in visualization tool for graphing metrics in TF. Common metrics to be visualized in `TensorBoard` include accuracy and loss. `TensorBoard` is compatible with TF1 and TF2 code. In TF1, `tf.estimator.Estimator` saves summaries for `TensorBoard` by default, and in TF2, summaries can be saved using `tf.keras.callbacks.TensorBoard`. In this example, you will see code that visualizes training progress using `TensorBoard` in TF1, followed by its equivalency in TF2."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "X74yjOb-e18w"
      },
      "outputs": [],
      "source": [
        "import tensorflow.compat.v1 as tf1\n",
        "import tensorflow as tf\n",
        "import numpy as np\n",
        "import datetime\n",
        "%load_ext tensorboard"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "2r8r4d8FfMny"
      },
      "outputs": [],
      "source": [
        "mnist = tf.keras.datasets.mnist\n",
        "\n",
        "(x_train, y_train),(x_test, y_test) = mnist.load_data()\n",
        "x_train, x_test = x_train / 255.0, x_test / 255.0"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wrqBkG4RFLP_"
      },
      "source": [
        "### TF1: MNIST Estimator training with visualization\n",
        "\n",
        "In this example, you will see a premade `tf.estimator.DNNClassifier` trained and evaluated on MNIST data and visualized using `Tensorboard`."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "upA8nuf3FEq5"
      },
      "outputs": [],
      "source": [
        "%reload_ext tensorboard\n",
        "\n",
        "feature_columns = [tf1.feature_column.numeric_column(\"x\", shape=[28, 28])]\n",
        "\n",
        "config = tf1.estimator.RunConfig(save_summary_steps=1,\n",
        "                                 save_checkpoints_steps=1)\n",
        "\n",
        "path = tempfile.mkdtemp()\n",
        "\n",
        "classifier = tf1.estimator.DNNClassifier(\n",
        "    feature_columns=feature_columns,\n",
        "    hidden_units=[256, 32],\n",
        "    optimizer=tf1.train.AdamOptimizer(0.001),\n",
        "    n_classes=10,\n",
        "    dropout=0.1,\n",
        "    model_dir=path,\n",
        "    config = config\n",
        ")\n",
        "\n",
        "train_input_fn = tf1.estimator.inputs.numpy_input_fn(\n",
        "    x={\"x\": x_train},\n",
        "    y=y_train.astype(np.int32),\n",
        "    num_epochs=10,\n",
        "    batch_size=50,\n",
        "    shuffle=True,\n",
        ")\n",
        "\n",
        "test_input_fn = tf1.estimator.inputs.numpy_input_fn(\n",
        "    x={\"x\": x_test},\n",
        "    y=y_test.astype(np.int32),\n",
        "    num_epochs=10,\n",
        "    shuffle=False\n",
        ")\n",
        "\n",
        "train_spec = tf1.estimator.TrainSpec(input_fn=train_input_fn, max_steps=10)\n",
        "eval_spec = tf1.estimator.EvalSpec(input_fn=test_input_fn,\n",
        "                                   steps=10,\n",
        "                                   throttle_secs=0)\n",
        "\n",
        "tf1.estimator.train_and_evaluate(estimator=classifier,\n",
        "                                train_spec=train_spec,\n",
        "                                eval_spec=eval_spec)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "EBqO7JbR8bh2"
      },
      "outputs": [],
      "source": [
        "%tensorboard --logdir {classifier.model_dir}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GK8TK1CU88ns"
      },
      "source": [
        "<!-- <img class=\"tfo-display-only-on-site\" src=\"images/tensorboard_TF1.png\"/> -->"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "QvE_uxDJFUX-"
      },
      "source": [
        "### TF2: Model training with visualization\n",
        "\n",
        "You will see a multi-layered `tf.keras.Model` trained with visualization using a `tf.keras.callbacks.TensorBoard`. In the TensorBoard graph you see the accuracy and loss by epoch when `Model.fit()` is called, which includes training and validation runs."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "9FLBhT2BFX2H"
      },
      "outputs": [],
      "source": [
        "%reload_ext tensorboard\n",
        "\n",
        "def create_model():\n",
        "  return tf.keras.models.Sequential([\n",
        "    tf.keras.layers.Flatten(input_shape=(28, 28)),\n",
        "    tf.keras.layers.Dense(512, activation='relu'),\n",
        "    tf.keras.layers.Dropout(0.2),\n",
        "    tf.keras.layers.Dense(10, activation='softmax')\n",
        "  ])\n",
        "\n",
        "model = create_model()\n",
        "model.compile(optimizer='adam',\n",
        "              loss='sparse_categorical_crossentropy',\n",
        "              metrics=['accuracy'],\n",
        "              steps_per_execution=10)\n",
        "\n",
        "log_dir = tempfile.mkdtemp()\n",
        "tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir,\n",
        "                                                      histogram_freq=1)\n",
        "\n",
        "model.fit(x=x_train,\n",
        "          y=y_train,\n",
        "          epochs=10,\n",
        "          validation_data=(x_test, y_test),\n",
        "          callbacks=[tensorboard_callback])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ovPoLrCJ8t-R"
      },
      "outputs": [],
      "source": [
        "%tensorboard --logdir {tensorboard_callback.log_dir}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Ip-IMGt_8xx9"
      },
      "source": [
        "<!-- <img class=\"tfo-display-only-on-site\" src=\"images/tensorboard_TF2.png\"/> -->\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rQUS8nO9FZlH"
      },
      "source": [
        "# Next Steps\n",
        "\n",
        "\n",
        "*   You can read more about `TensorBoard` at https://www.tensorflow.org/tensorboard/get_started \n",
        "*   If you're interested in lower level APIs see the `tf.summary` migration guide https://www.tensorflow.org/tensorboard/migrate\n"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "name": "tensorboard.ipynb",
      "provenance": [],
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
